package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: hamo
 * @Create by: 渭北彭于晏
 * @Date: 2021/11/7 14:43
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @Override
    public void submit(Orders orders) {
        //补齐信息
        //获取user_id
        Long userid = BaseContext.getCurrentId();
        //根据userid获取用户购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userid);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        //获取地址相关信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        String phone = addressBook.getPhone();
        String detail = addressBook.getDetail();
        String consignee = addressBook.getConsignee();
        BigDecimal bigDecimal = sumAmount(list);

        //获取当前用户数据
        User user = userService.getById(userid);
        String name = user.getName();

        orders.setNumber(IdWorker.get32UUID());
        orders.setUserId(userid);
        orders.setAmount(bigDecimal);
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setOrderTime(LocalDateTime.now());
        orders.setPhone(phone);
        orders.setAddress(detail);
        orders.setUserName(name);
        orders.setConsignee(consignee);

        save(orders);//保存订单


        for (ShoppingCart shoppingCart : list) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderDetailService.save(orderDetail);//保存订单明细
        }




        //清购物车
        shoppingCartService.remove(wrapper);
    }

    private BigDecimal sumAmount(List<ShoppingCart> list) {
        BigDecimal bigDecimal = new BigDecimal(0.0);
        for (ShoppingCart cart : list) {
            BigDecimal amount = cart.getAmount();
            Integer number = cart.getNumber();
            BigDecimal multiply = amount.multiply(new BigDecimal(number));
            bigDecimal = bigDecimal.add(multiply);
        }
        return bigDecimal;
    }
}
